﻿<template>
  <div v-loading = 'loading' v-if="isReady">
    <V-Settings v-if="ruleForm.uid !== ''" :currentRow = 'ruleForm' :showEditContentDialog = 'dialogFormVisible' from = 'dynamic' :commonData="commonData" @resetForm = 'resetForm' @submitForm = 'submitForm'></V-Settings>
  </div>
</template>

<script>
import VSettings from '@/components/DynamicFormComponents_v2/DynamicForm.vue'
export default {
  data () {
    return {
      loading: false,
      dialogFormVisible: true,
      // 当前的uid
      currentUid: '',
      // 表单规则
      ruleForm: {
        name: '',
        desc: '',
        settings: '',
        route: '',
        category: '',
        uid: ''
      },
      isReady: false,
      commonData: {
        metaList: [],
        metaList_inc_fc: [],
        alltables: [],
        dictables: [],
        apiEvents: []
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      let self = this
      this.currentUid = this.$route.query.uid
      self.isReady = false
      self.$api.getAllSchema(false).then(data => {
        self.commonData.alltables = data.data
        self.$api.getMetaData(false, false).then(data => {
          self.commonData.metaList = data
          self.$api.getMetaData(true, false).then(data => {
            self.commonData.metaList_inc_fc = data
            self.$api.getDictionary(false).then(data => {
              self.commonData.dictables = data
              self.$api.getEvents4DForm(false).then((data) => {
                self.commonData.apiEvents = data
                self.$store.dispatch('setCommonData', self.commonData)
                self.isReady = true
              })
            })
          })
        })
      })
      this.getCurrentPageInfo()
    },
    resetForm () {
      this.$emit('giveUp')
    },
    submitForm (fromSettings) {
      this.ruleForm.settings = fromSettings.jsonData
      this.$api.send('/d_form/' + this.currentUid, {
        name: this.ruleForm.name,
        desc: this.ruleForm.desc,
        settings: this.ruleForm.settings,
        route: this.ruleForm.route,
        category: this.ruleForm.category,
        type: 'normal_v2'
      }, 'patch').then((data) => {
        this.$router.back()
      })
    },
    // 获取指定页面的数据
    getCurrentPageInfo () {
      this.$api.query('/d_form/' + this.currentUid).then((data) => {
        this.ruleForm.name = data.name
        this.ruleForm.desc = data.desc
        this.ruleForm.settings = data.settings
        // 保存历史版本的setting
        this.currentPageSetting = data.settings
        this.ruleForm.route = data.route
        this.ruleForm.category = data.category
        this.ruleForm.uid = data.uid
      })
    }
  },
  components: {
    VSettings
  },
  watch: {
    showSettingsDialog: {
      handler: function (val, oldVal) {
        this.dialogFormVisible = val
      },
      deep: true
    }
  }
}
</script>
